/**
 * 
 */
package com.gragra.sampling;
import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
/**
 * @author Christoph Teichmann
 * 12.06.2013
 */
public class LogarithmicFactorialPlusAlphaCash implements LogarithmicFactorialCash
{
	/**
	 * 
	 */
	private final DoubleArrayList[] lists; 
	/**
	 * 
	 */
	private final double alpha;
	/* (non-Javadoc)
	 * @see com.gragra.sampling.LogarithmicFactorialCash#getAlpha()
	 */
	@Override
	public double getAlpha()
	{return this.alpha;}
	/**
	 * @param alpha
	 */
	public LogarithmicFactorialPlusAlphaCash(double alpha,int threads)
	{
		super();
		this.alpha = alpha;
		this.lists = new DoubleArrayList[threads];
		for(int i=0;i<threads;++i)
		{addList(i);}
	}
	/**
	 * @param thread 
	 * 
	 */
	private void addList(int thread)
	{
		this.lists[thread] = new DoubleArrayList();
		this.lists[thread].add(Math.log(alpha));
	}
	/* (non-Javadoc)
	 * @see com.gragra.sampling.LogarithmicFactorialCash#getValue(int, int)
	 */
	@Override
	public double getValue(int amount,int thread)
	{
		DoubleArrayList dal = this.lists[thread];
		while(dal.size() <= amount)
		{
			int size = dal.size();
			dal.add(Math.log(((double) size)+alpha)+dal.get(size-1));
		}
		return dal.getDouble(amount);
	}
}